app.controller('goodsCtrl', ['$scope', '$state', '$cookies', 'goodsService', 'utilService', 'toaster', '$modal', function ($scope, $state, $cookies, goodsService, utilService, toaster, $modal) {
    $scope.brands;
    $scope.types;

    $scope.goodsListVO = {}

    //查询商品列表
    $scope.findPageAll = function () {
        utilService.queryForPagingList($scope, {
            url: $scope.app.domain + 'goods',
            params: $scope.goodsListVO
        }).then(function (result) {
        });
    }

    $scope.findPageAll();

    //查询品牌信息
    $scope.querybrands = function () {
        goodsService.querybrands($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.brands = result.data;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    }

    $scope.querybrands();

    //查询类型信息
    $scope.queryType = function () {
        goodsService.queryType($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.types = result.data;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    }

    $scope.queryType();

    $scope.batchStatus=false;
    //批量上架
    $scope.batchUpdateUpStatus = function () {
        $scope.batchStatus=true;
        goodsService.batchUpdateUpStatus($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.types = result.data;
                    $scope.findPageAll();
                    toaster.pop('success', "提示", "操作成功");
                    $scope.batchStatus=false;
                    break;
                case 677:
                    $scope.batchStatus=false;
                    break;
                case 678:
                    toaster.pop('waiting', "提示", result.message);
                    $scope.batchStatus=false;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    $scope.batchStatus=false;
                    break;
            }
        })
    }

    //批量下架
    $scope.batchUpdateDownStatus = function () {
        $scope.batchStatus=true;
        goodsService.batchUpdateDownStatus($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.types = result.data;
                    $scope.findPageAll();
                    toaster.pop('success', "提示", "操作成功");
                    $scope.batchStatus=false;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    $scope.batchStatus=false;
                    break;
            }
        })
    }


    //更新上下架,申请再次审核
    $scope.updateStatus = function (id, status) {

        goodsService.updateStatus($scope, id, status).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.types = result.data;
                    $scope.findPageAll();
                    toaster.pop('success', "提示", "操作成功");
                    break;
                case 677:
                case 678:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })

    }

    //批量选择
    $scope.selected = [];

    $scope.isChecked = function (item) {
        return $scope.selected.indexOf(item) >= 0;
    };

    $scope.updateSelection = function ($event, item) {

        var checkbox = $event.target;
        var checked = checkbox.checked;

        if (checked) {
            $scope.selected.push(item);
        } else {
            var idx = $scope.selected.indexOf(item);
            $scope.selected.splice(idx, 1);
        }
    };

    $scope.isCheckedAll = function () {

        if ($scope.list == null) {
            return;
        }

        return $scope.selected.length == $scope.list.length;
    }

    $scope.checkAll = function ($event) {
        var checkbox = $event.target;
        var checked = checkbox.checked;

        for (var i = 0; i < $scope.list.length; i++) {
            if (checked) {
                $scope.selected.push($scope.list[i]);
            } else {
                $scope.selected.splice(0, $scope.selected.length);
            }
        }
    }

    $scope.initSelect = function () {
        $scope.selected = [];
    }

    //
    $scope.deleteGoods = function (id, name) {
        var param = {
            id: id,
            name: name
        };

        openModal("你是否要删除[" + name + "]商品？删除后将无法恢复");

        function openModal(msg) {
            utilService.modal($modal, $scope, {
                title: "删除商品",
                controller: "deleteGoodsCtrl",
                message: msg,
                data: param
            });
        }
    }

    //批量删除商品
    $scope.batchDeleteGoods = function () {
        openModal("你是否要批量删除选中的商品?删除后将无法恢复");

        function openModal(msg) {
            utilService.modal($modal, $scope, {
                title: "删除商品",
                controller: "batchDeleteGoodsCtrl",
                message: msg,
                data: $scope.selected
            })
        }
    }

    //打开查看服务框
    $scope.checkReserve = function (id) {
        var modalInstance = $modal.open({
            templateUrl: 'tpl/goods/reserve_info.html',
            controller: 'reserveInfoCtrl',
            size: 'md',
            scope:$scope,
            resolve: {
                data: function () {
                    return id;
                }
            }
        });

        modalInstance.result.then(function (selectedItem) {
            $scope.selected = selectedItem;
        }, function () {
            console.info('Modal dismissed at: ' + new Date());
        });
    };

}]);

app.controller('reserveInfoCtrl',['$scope','toaster','data','$modalInstance','goodsService',
    function($scope,toaster,data,$modalInstance,goodsService){
    $scope.id=data;
    $scope.goodsInfo={};

    $scope.checkReserve=function(){
        goodsService.checkReserve($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.goodsInfo=result.data;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    };

    $scope.checkReserve();

    $scope.ok = function(){
        $modalInstance.dismiss('cancel');
    };

}]);

app.controller('deleteGoodsCtrl', ['$scope', '$modalInstance', '$state', 'goodsService', 'toaster', 'modalData',
    function ($scope, $modalInstance, $state, goodsService, toaster, modalData) {
        $scope.modal = modalData;

        $scope.goods = {
            id: modalData.data.id
        };
        $scope.isDelModal = true;

        //确认删除
        $scope.ok = function () {
            if (!$scope.isDelModal) {
                return;
            }

            $scope.isDelModal = false;

            goodsService.delete($scope).then(function (result) {
                switch (result.status) {
                    case 200:
                        $modalInstance.dismiss('success');
                        $scope.findPageAll();
                        toaster.pop('success', "提示", "操作成功");
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        }

        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }
]);

app.controller('batchDeleteGoodsCtrl', ['$scope', 'modalData', '$modalInstance','goodsService','toaster',
    function ($scope, modalData, $modalInstance,goodsService,toaster) {
        $scope.modal = modalData;
        $scope.ids = [];
        for (var i = 0; i < modalData.data.length; i++) {
            $scope.ids.push(modalData.data[i].id);
        }
        $scope.isDelModal = true;
        $scope.ok = function () {
            if (!$scope.isDelModal) {
                return;
            }
            $scope.isDelModal = false;
            goodsService.batchDelete($scope).then(
                function (result) {
                    switch (result.status) {
                        case 200:
                            $modalInstance.dismiss('success');
                            $scope.findPageAll();
                            toaster.pop('success', "提示", "操作成功");
                            break;
                        default:
                            toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                            break;
                    }
                }
            );
        };
        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        }
    }
]);